#include "TaskDesc.h"
#include "../../model/vo/ViewVO.h"
#include "../../utils/LocalString.h"
#include "../../model/static/StaticDungeons.h"
#include "../../constants/GameConstans.h"
#include "../../model/proxy/DungeonsProxy.h"
#include "../../model/proxy/FightProxy.h"
#include "../../net/NetController.h"
#include "TaskDescLoader.h"

TaskDesc::TaskDesc(void)
	:mName(NULL), mDesc(NULL), mExp(NULL), mCoin(NULL), mEnergy(NULL)
{
}


TaskDesc::~TaskDesc(void)
{
	CC_SAFE_RELEASE(mName);
	CC_SAFE_RELEASE(mDesc);
	CC_SAFE_RELEASE(mExp);
	CC_SAFE_RELEASE(mCoin);
	CC_SAFE_RELEASE(mEnergy);
}

bool TaskDesc::onAssignCCBMemberVariable( cocos2d::CCObject * pTarget, cocos2d::CCString * pMemberVariableName, cocos2d::CCNode * pNode )
{
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mDesc", CCLabelTTF *, this->mDesc);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mName", CCLabelTTF *, this->mName);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mExp", CCLabelTTF *, this->mExp);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mCoin", CCLabelTTF *, this->mCoin);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mEnergy", CCLabelTTF *, this->mEnergy);
	return false;
}

void TaskDesc::onDoItBtnClick(CCObject * pSender, CCControlEvent pCCControlEvent)
{
	//For Test
	NetController::getSingletonPtr()->dungeonExplore(
		DungeonsProxy::getSingletonPtr()->getCurDungeonsID(),
		DungeonsProxy::getSingletonPtr()->getCurFloorID(),
		DungeonsProxy::getSingletonPtr()->getCurTaskID());

	FightProxy::getSingletonPtr()->dungeons = DungeonsProxy::getSingletonPtr()->getCurDungeonsID();
	FightProxy::getSingletonPtr()->floor = DungeonsProxy::getSingletonPtr()->getCurFloorID();
	FightProxy::getSingletonPtr()->task = DungeonsProxy::getSingletonPtr()->getCurTaskID();
	//
	int progress = DungeonsProxy::getSingletonPtr()->curTaskInfo.progress+=30;
	if(progress >= 100)
	{
		DungeonsProxy::getSingletonPtr()->curTaskInfo.task++;
		DungeonsProxy::getSingletonPtr()->curTaskInfo.progress = 0;
	}
	EquipInfo* ei = new EquipInfo();
	ei->atkMin = 10;
	ei->atkMax = 20;
	ei->defMin = 10;
	ei->defMax = 10;
	ei->exp = 100;
	ei->id = 30001;
	ei->index  = 6;
	ei->level = 3;
	ei->life = 20;
	ei->state = 0;
	Award& award = FightProxy::getSingletonPtr()->awardInfo;
	award.clear();
	award.coin = 10;
	award.exp = 10;
	award.equipList.push_back(ei);
	FightProxy::getSingletonPtr()->battleInfo.win = 1;
	Post_Net_Notification(kNCDungeonExplore, NULL);
}

bool TaskDesc::init()
{
	return true;
}

void TaskDesc::refresh()
{
	TaskStatic* taskInfo = DungeonsProxy::getSingletonPtr()->getCurTaskStatic();
	mName->setString(g2u(taskInfo->name));
	mDesc->setString(g2u(taskInfo->desc));
	mExp->setString(fcs("%d-%d", taskInfo->expMin, taskInfo->expMax));
	mCoin->setString(fcs("%d-%d", taskInfo->coinMin, taskInfo->coinMax));
	mEnergy->setString(fcs("%d", taskInfo->energy));
	mProgress->setPecent(float(DungeonsProxy::getSingletonPtr()->getCurProgress()));
}

cocos2d::SEL_MenuHandler TaskDesc::onResolveCCBCCMenuItemSelector( cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName )
{
	return NULL;
}

cocos2d::extension::SEL_CCControlHandler TaskDesc::onResolveCCBCCControlSelector( cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onDoItBtnClick", TaskDesc::onDoItBtnClick);
	return NULL;
}

void TaskDesc::onEnter()
{
	CCNode::onEnter();
}

void TaskDesc::onExit()
{
	CCNode::onExit();
}

void TaskDesc::onNodeLoaded( cocos2d::CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader )
{
	mProgress = ProgressText::create("sprite/progress_01.png",gls("font_yahei"),14);
	mProgress->setFormatStr(gls("complete_pecent"));
	mProgress->setRect(0, 100);
	mProgress->setPecent(100);
	mProgress->setPosition(160, 65);
	mProgress->getText()->setPosition(ccp(0,0));
	addChild(mProgress);
    CCSize size(292,40);
    mDesc->setDimensions(size);
}

void TaskDesc::_onNotification(CCObject* object)
{
}

TaskDesc* TaskDesc::create(CCObject* pOwer)
{
	RegisterLoader("TaskDesc", TaskDescLoader::loader());
	return (TaskDesc*)(ReadNode("TaskDesc.ccbi", pOwer));
}


